/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2009>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using MyPhotoIndex.Controls;
using MyPhotoIndex.Settings;

namespace MyPhotoIndex
{
    public partial class ApplicationSettingsForm : FormTemplate
    {
        private String m_settingToLocate;

        public ApplicationSettingsForm()
        {
            InitializeComponent();

            listBox1.DrawItem += new DrawItemEventHandler(listBox1_DrawItem);
        }

        internal static void ShowAndHighliteSettting(IWin32Window owner, String settingToLocate)
        {
            ApplicationSettingsForm asf = new ApplicationSettingsForm();
            asf.StartPosition = FormStartPosition.CenterParent;
            asf.m_settingToLocate = settingToLocate;
            asf.ShowDialog(owner);
        }

        void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            Graphics graphics = e.Graphics;
            Rectangle nodeRect = e.Bounds;

            bool isSelected = (e.Index == listBox1.SelectedIndex);

            Color borderColor = Color.FromArgb(101, 147, 207);
            using (Pen borderPen = new Pen(borderColor))
            {
                Point start = new Point(0, 0);
                Point end = new Point(nodeRect.Width, 0);
                graphics.DrawLine(borderPen, start, end);

                start = new Point(0, nodeRect.Bottom);
                end = new Point(nodeRect.Width, nodeRect.Bottom);
                graphics.DrawLine(borderPen, start, end);
            }

            Color topFirstColor, topSecondColor, bottomFirstColor, bottomSecondColor;
            if (isSelected == false)
            {
                topFirstColor = Color.FromArgb(227, 239, 255);
                topSecondColor = Color.FromArgb(196, 221, 255);
                bottomFirstColor = Color.FromArgb(173, 209, 255);
                bottomSecondColor = Color.FromArgb(192, 219, 255);
            }
            else
            {
                topFirstColor = Color.FromArgb(255, 217, 170);
                topSecondColor = Color.FromArgb(255, 187, 110);
                bottomFirstColor = Color.FromArgb(255, 171, 63);
                bottomSecondColor = Color.FromArgb(254, 225, 122);
            }

            Rectangle topDrawRect = new Rectangle(0, nodeRect.Top + 1, nodeRect.Width, 12);
            Rectangle bottomDrawRect = new Rectangle(0, topDrawRect.Bottom, nodeRect.Width, nodeRect.Height - topDrawRect.Height - 1);

            using (Brush brush = new LinearGradientBrush(topDrawRect,
                                topFirstColor, topSecondColor, LinearGradientMode.Vertical))
            {
                graphics.FillRectangle(brush, topDrawRect);
            }

            using (Brush brush = new LinearGradientBrush(bottomDrawRect,
                        bottomFirstColor, bottomSecondColor, LinearGradientMode.Vertical))
            {
                graphics.FillRectangle(brush, bottomDrawRect);
            }

            Color textColor = Color.FromArgb(32, 77, 137);
            using (Brush textBrush = new SolidBrush(textColor))
            {
                Object listItem = listBox1.Items[e.Index];
                String text = listItem.ToString();

                SizeF textSize = graphics.MeasureString(text, e.Font);
                Point textPoint = new Point(0, (int)((nodeRect.Y + ((nodeRect.Height - textSize.Height) / 2))));
                graphics.DrawString(text, e.Font, textBrush, textPoint);
            }
        }

        private void ApplicationSettings_Load(object sender, EventArgs e)
        {
            listBox1.Items.AddRange(DynamicConfiguration.Default.Settings);
            listBox1.SelectedIndex = 0;

            if (String.IsNullOrEmpty(m_settingToLocate) == false)
            {
                propertyGrid1.SelectSetting(m_settingToLocate);
            }
        }

        protected override void OkButton_Click(object sender, EventArgs e)
        {
            ApplicationSettings.Default.Save();
            base.OkButton_Click(sender, e);
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBox1.Refresh();
            propertyGrid1.SelectedObject = ((ListBox)sender).SelectedItem;
            propertyGrid1.Refresh();
        }
    }
}

